package com.gonsin.project.creator.list;

import com.gonsin.project.creator.utils.ProjectEnums;
import com.gonsin.project.creator.utils.TableUtils;
import freemarker.core.TemplateRTFOutputModel;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.gonsin.project.creator.Main.p;

/**
 * 列出所有模板，用法，说明
 */
public class ListCmd {


    public void run(String[] args) throws IOException {

        String jarPath = args[0].trim();
        File templateRoot = new File(jarPath + File.separator + "templates");

        List<TemplatePo> tempList = new ArrayList<>();

        File[] list = templateRoot.listFiles();
        for(File f : list){
            if(isTemplatePath(f)){
                TemplatePo t = new TemplatePo();
                t.setTemplateContent(findTemplateContent(f));
                t.setTemplateName(f.getName());
                tempList.add(t);
            }
        }

        TableUtils table = TableUtils.create();
        table.setSbcMode(false);
        table.addHeader("模板名字", "使用方法", "模板说明");
        for(TemplatePo t : tempList){
            table.addBody(t.getTemplateName(), "gproject create " + t.getTemplateName(), t.getTemplateContent());
        }
        table.print();
    }

    private String findTemplateContent(File f) throws IOException {
        File remark = new File(f.getAbsolutePath() + File.separator + ProjectEnums.REMARK);
        String content = FileUtils.readFileToString(remark, "UTF8");
        return content;
    }


    /**
     * 是否为模板目录
     */
    private boolean isTemplatePath(File f) {
        if(!new File(f.getAbsolutePath() + File.separator + ProjectEnums.INPUT_JS).exists()){
            return false;
        }
        if(!new File(f.getAbsolutePath() + File.separator + ProjectEnums.REMARK).exists()){
            return false;
        }

        return true;
    }
}
